package com.xdog.okx.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.alibaba.fastjson.JSONObject;
import com.xdog.common.core.domain.R;
import com.xdog.okx.domain.address.*;
import com.xdog.okx.service.IOkxAddressService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * OKX 地址数据控制器
 */
@Tag(name = "OKX 地址数据", description = "OKX 地址数据控制器")
@RestController
@RequestMapping("/okx/address")
public class OkxAddressController {

    @Resource
    private IOkxAddressService okxAddressService;

    /**
     * 查询地址详情
     */
    @Operation(summary = "查询地址详情", description = "根据链短名称和地址查询地址详情")
    @GetMapping("/info")
    @SaIgnore
    public R<JSONObject> getAddressInfo(AddressInfoQuery query) {
        try {
            Map<String, String> params = new HashMap<>();
            params.put("chainShortName", query.getChainShortName());
            params.put("address", query.getAddress());

            JSONObject addressInfo = okxAddressService.getAddressInfo(params);
            return R.ok(addressInfo);
        } catch (Exception e) {
            return R.fail("查询地址详情失败: " + e.getMessage());
        }
    }

    /**
     * 查询地址代币余额详情
     */
    @Operation(summary = "查询地址代币余额详情", description = "根据链短名称和地址查询代币余额详情")
    @GetMapping("/token-balance")
    public R<JSONObject> getAddressTokenBalance(AddressTokenBalanceQuery query) {
        try {
            Map<String, String> params = new HashMap<>();
            params.put("chainShortName", query.getChainShortName());
            params.put("address", query.getAddress());
            params.put("protocolType", query.getProtocolType());
            if (query.getTokenContractAddress() != null) params.put("tokenContractAddress", query.getTokenContractAddress());
            if (query.getPage() != null) params.put("page", query.getPage());
            if (query.getLimit() != null) params.put("limit", query.getLimit());

            JSONObject tokenBalance = okxAddressService.getAddressTokenBalance(params);
            return R.ok(tokenBalance);
        } catch (Exception e) {
            return R.fail("查询地址代币余额详情失败: " + e.getMessage());
        }
    }

    /**
     * 查询地址交易列表
     */
    @Operation(summary = "查询地址交易列表", description = "根据链短名称和地址查询交易列表")
    @GetMapping("/transaction-list")
    public R<JSONObject> getAddressTransactionList(AddressTransactionListQuery query) {
        try {
            Map<String, String> params = new HashMap<>();
            params.put("chainShortName", query.getChainShortName());
            params.put("address", query.getAddress());
            if (query.getProtocolType() != null) params.put("protocolType", query.getProtocolType());
            if (query.getTokenContractAddress() != null) params.put("tokenContractAddress", query.getTokenContractAddress());
            if (query.getStartBlockHeight() != null) params.put("startBlockHeight", query.getStartBlockHeight());
            if (query.getEndBlockHeight() != null) params.put("endBlockHeight", query.getEndBlockHeight());
            if (query.getIsFromOrTo() != null) params.put("isFromOrTo", query.getIsFromOrTo());
            if (query.getPage() != null) params.put("page", query.getPage());
            if (query.getLimit() != null) params.put("limit", query.getLimit());

            JSONObject transactionList = okxAddressService.getAddressTransactionList(params);
            return R.ok(transactionList);
        } catch (Exception e) {
            return R.fail("查询地址交易列表失败: " + e.getMessage());
        }
    }

    /**
     * 查询地址普通交易列表
     */
    @Operation(summary = "查询地址普通交易列表", description = "根据链短名称和地址查询普通交易列表")
    @GetMapping("/normal-transaction-list")
    public R<JSONObject> getAddressNormalTransactionList(AddressNormalTransactionListQuery query) {
        try {
            Map<String, String> params = new HashMap<>();
            params.put("chainShortName", query.getChainShortName());
            params.put("address", query.getAddress());
            if (query.getStartBlockHeight() != null) params.put("startBlockHeight", query.getStartBlockHeight());
            if (query.getEndBlockHeight() != null) params.put("endBlockHeight", query.getEndBlockHeight());
            if (query.getIsFromOrTo() != null) params.put("isFromOrTo", query.getIsFromOrTo());
            if (query.getPage() != null) params.put("page", query.getPage());
            if (query.getLimit() != null) params.put("limit", query.getLimit());

            JSONObject normalTransactionList = okxAddressService.getAddressNormalTransactionList(params);
            return R.ok(normalTransactionList);
        } catch (Exception e) {
            return R.fail("查询地址普通交易列表失败: " + e.getMessage());
        }
    }

    /**
     * 查询地址内部交易列表
     */
    @Operation(summary = "查询地址内部交易列表", description = "根据链短名称和地址查询内部交易列表")
    @GetMapping("/internal-transaction-list")
    public R<JSONObject> getAddressInternalTransactionList(AddressInternalTransactionListQuery query) {
        try {
            Map<String, String> params = new HashMap<>();
            params.put("chainShortName", query.getChainShortName());
            params.put("address", query.getAddress());
            if (query.getStartBlockHeight() != null) params.put("startBlockHeight", query.getStartBlockHeight());
            if (query.getEndBlockHeight() != null) params.put("endBlockHeight", query.getEndBlockHeight());
            if (query.getIsFromOrTo() != null) params.put("isFromOrTo", query.getIsFromOrTo());
            if (query.getPage() != null) params.put("page", query.getPage());
            if (query.getLimit() != null) params.put("limit", query.getLimit());

            JSONObject internalTransactionList = okxAddressService.getAddressInternalTransactionList(params);
            return R.ok(internalTransactionList);
        } catch (Exception e) {
            return R.fail("查询地址内部交易列表失败: " + e.getMessage());
        }
    }

    /**
     * 查询地址代币交易列表
     */
    @Operation(summary = "查询地址代币交易列表", description = "根据链短名称和地址查询代币交易列表")
    @GetMapping("/token-transaction-list")
    public R<JSONObject> getAddressTokenTransactionList(AddressTokenTransactionListQuery query) {
        try {
            Map<String, String> params = new HashMap<>();
            params.put("chainShortName", query.getChainShortName());
            params.put("address", query.getAddress());
            params.put("protocolType", query.getProtocolType());
            if (query.getTokenContractAddress() != null) params.put("tokenContractAddress", query.getTokenContractAddress());
            if (query.getStartBlockHeight() != null) params.put("startBlockHeight", query.getStartBlockHeight());
            if (query.getEndBlockHeight() != null) params.put("endBlockHeight", query.getEndBlockHeight());
            if (query.getIsFromOrTo() != null) params.put("isFromOrTo", query.getIsFromOrTo());
            if (query.getPage() != null) params.put("page", query.getPage());
            if (query.getLimit() != null) params.put("limit", query.getLimit());

            JSONObject tokenTransactionList = okxAddressService.getAddressTokenTransactionList(params);
            return R.ok(tokenTransactionList);
        } catch (Exception e) {
            return R.fail("查询地址代币交易列表失败: " + e.getMessage());
        }
    }

    /**
     * 批量查询指定区块普通交易列表
     */
    @Operation(summary = "批量查询指定区块普通交易列表", description = "根据链短名称和地址批量查询指定区块普通交易列表")
    @GetMapping("/normal-transaction-list-multi")
    public R<JSONObject> getNormalTransactionListMulti(NormalTransactionListMultiQuery query) {
        try {
            Map<String, String> params = new HashMap<>();
            params.put("chainShortName", query.getChainShortName());
            params.put("address", query.getAddress());
            params.put("startBlockHeight", query.getStartBlockHeight());
            params.put("endBlockHeight", query.getEndBlockHeight());
            if (query.getIsFromOrTo() != null) params.put("isFromOrTo", query.getIsFromOrTo());
            if (query.getPage() != null) params.put("page", query.getPage());
            if (query.getLimit() != null) params.put("limit", query.getLimit());

            JSONObject normalTransactionListMulti = okxAddressService.getNormalTransactionListMulti(params);
            return R.ok(normalTransactionListMulti);
        } catch (Exception e) {
            return R.fail("批量查询指定区块普通交易列表失败: " + e.getMessage());
        }
    }

    /**
     * 批量查询指定区块内部交易列表
     */
    @Operation(summary = "批量查询指定区块内部交易列表", description = "根据链短名称和地址批量查询指定区块内部交易列表")
    @GetMapping("/internal-transaction-list-multi")
    public R<JSONObject> getInternalTransactionListMulti(InternalTransactionListMultiQuery query) {
        try {
            Map<String, String> params = new HashMap<>();
            params.put("chainShortName", query.getChainShortName());
            params.put("address", query.getAddress());
            params.put("startBlockHeight", query.getStartBlockHeight());
            params.put("endBlockHeight", query.getEndBlockHeight());
            if (query.getPage() != null) params.put("page", query.getPage());
            if (query.getLimit() != null) params.put("limit", query.getLimit());

            JSONObject internalTransactionListMulti = okxAddressService.getInternalTransactionListMulti(params);
            return R.ok(internalTransactionListMulti);
        } catch (Exception e) {
            return R.fail("批量查询指定区块内部交易列表失败: " + e.getMessage());
        }
    }

    /**
     * 查询主链币持仓地址列表
     */
    @Operation(summary = "查询主链币持仓地址列表", description = "根据链短名称查询主链币持仓地址列表")
    @GetMapping("/native-token-position-list")
    public R<JSONObject> getNativeTokenPositionList(NativeTokenPositionListQuery query) {
        try {
            Map<String, String> params = new HashMap<>();
            params.put("chainShortName", query.getChainShortName());
            if (query.getPage() != null) params.put("page", query.getPage());
            if (query.getLimit() != null) params.put("limit", query.getLimit());

            JSONObject nativeTokenPositionList = okxAddressService.getNativeTokenPositionList(params);
            return R.ok(nativeTokenPositionList);
        } catch (Exception e) {
            return R.fail("查询主链币持仓地址列表失败: " + e.getMessage());
        }
    }
}